<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    用户名： <input type="text"><span id="tip"></span><br>
    <button id="btn">点击按钮查看用户名是否可用</button>
<script>
const btn = document.querySelector("#btn")
const tip = document.querySelector("#tip")
btn.onclick = function (){
    const name = document.querySelector("input[type=text]").value.trim()
    const xhr = new XMLHttpRequest()
    xhr.open("get","http://localhost:5000/check?name=" + name)
    xhr.send(null)
    xhr.onreadystatechange = function (){
        if(xhr.readyState === 4){
            if(xhr.status === 200){
                console.log("请求响应成功了")
                const data = JSON.parse(xhr.responseText)
                if(data.success){
                    tip.innerText = data.msg
                    tip.style.color = "green"
                }else{
                    tip.innerText = data.msg
                    tip.style.color = "red"
                }
            }else {
              console.log('请求响应失败了')
            }
        }
    }
}

console.log('事件注册完了')
</script>
</body>
</html>